package cn.hongxin.sap.conn.client;

import cn.getech.poros.framework.common.api.RestResponse;
import cn.hongxin.sap.conn.dto.ErpFunctionParamDTO;
import cn.hongxin.sap.conn.dto.ErpFunctionReturnDto;
import cn.hongxin.sap.conn.util.SapUtil;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * WMS 专用SAP接口，请勿随意调整
 * @author bocong.zheng@getech.cn
 * @since 2020/11/5
 */
@FeignClient(
        name = "sap-interface",
        path = "/api/sap-interface/sap"
)
@Validated
public interface WmsSapClient {

    @RequestMapping(value = "/callSapInterface",method = RequestMethod.POST)
    RestResponse<ErpFunctionReturnDto> callSapInterface(@RequestParam String functionName, @RequestParam String paramJsonStr);

    @RequestMapping(value = "/retryCallSapInterface",method = RequestMethod.POST)
    RestResponse<ErpFunctionReturnDto> retryCallSapInterface(@RequestParam String functionName, @RequestParam String paramJsonStr, @RequestParam String prodQueueId);

    @RequestMapping(value = "/v1/callSapInterface",method = RequestMethod.POST)
    RestResponse<ErpFunctionReturnDto> invokingSapInterface(@RequestBody ErpFunctionParamDTO erpFunctionParamDTO);

     /**
     *  sap 调用逻辑封装
     * @param <T>
     * @param functionId       接口名称
     * @param param            请求参数
     * @param clz              出参类型
     * @return
     */
    default <T> RestResponse<T> callSapInterface(String functionId, Object param, Class<T> clz) {
        ErpFunctionParamDTO erpFunctionParamDTO = new ErpFunctionParamDTO();
        erpFunctionParamDTO.setFunctionName(functionId);
        erpFunctionParamDTO.setParamJson(SapUtil.convertInput(param));
        RestResponse<ErpFunctionReturnDto> restResponse = this.invokingSapInterface(erpFunctionParamDTO);
        if(!restResponse.isSuccess()){
            return RestResponse.failed(restResponse.getMsg());
        }
        return RestResponse.ok(SapUtil.convertOutput(restResponse.getData(),clz));
    }

}
